package com.itHuangTao.tank.ui;

import com.almasb.fxgl.animation.Interpolators;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.scene.SubScene;
import com.almasb.fxgl.texture.Texture;
import javafx.animation.TranslateTransition;
import javafx.util.Duration;

//游戏失败场景
public class FailedScene extends SubScene {

    private final TranslateTransition tt;
    private final Texture texture;

    public FailedScene() {
        texture = FXGL.texture("GameOver.png");
        texture.setLayoutX(28 * 24 / 2.0 - texture.getWidth() / 2.0);
        texture.setLayoutY(FXGL.getAppHeight());
        //动画弹出失败页面(2.6秒时间)
        tt = new TranslateTransition(Duration.seconds(2.6), texture);
        //弹出动画设置为弹跳效果
        tt.setInterpolator(Interpolators.ELASTIC.EASE_OUT());
        //动画图片弹出到页面的位置
        tt.setFromY(0);
        tt.setToY(-(FXGL.getAppHeight() - 260));
        //播放完成后把页面恢复到原来页面(更换位置隐藏图片)
        tt.setOnFinished(event->{
            //关闭子场景
            FXGL.getSceneService().popSubScene();
            texture.setTranslateY(0);
            //跳回主菜单
           FXGL.getGameController().gotoMainMenu();
        });
        //加入组件图片
        getContentRoot().getChildren().add(texture);
    }

    @Override
    public void onCreate() {
        tt.play();
    }
}
